import { _decorator, Component, ConeCollider, director, find, instantiate, Label, math, Node, Prefab, Quat, Sprite, Tween, tween, UI, UITransform, v3, Vec3, view } from 'cc';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import Banner from '../Banner';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { GameManager } from '../GameManager';
import { NodeUtil } from '../AshFramework/Utils/NodeUtil';
import { Panel, UIManager } from '../AshFramework/Managers/UIManager';
import { ShopPanel } from './ShopPanel';
import { HeroPanel } from './HeroPanel';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { CharacterState, PlayerController } from '../PlayerController';
import { ZTool } from '../AshFramework/Utils/ZTool';

const { ccclass, property } = _decorator;

@ccclass('StartPanel')
export class StartPanel extends Component {
    static Instance: StartPanel;
    Panel: Node;
    Menus: Node;
    DeckButton: Node;
    MoneyLabel: Label;
    menuButtonSps: Sprite[] = [];

    shopPanel: ShopPanel;
    heroPanel: HeroPanel;

    hero: Node;

    protected onLoad(): void {
        StartPanel.Instance = this;
        this.Panel = NodeUtil.GetNode("Panel", this.node);
        this.Menus = NodeUtil.GetNode("Menus", this.node);
        this.DeckButton = NodeUtil.GetNode("DeckButton", this.node);
        this.MoneyLabel = NodeUtil.GetComponent("MoneyLabel", this.node, Label);

        this.menuButtonSps.push(NodeUtil.GetComponent("WeaponsButton", this.node, Sprite));
        this.menuButtonSps.push(NodeUtil.GetComponent("PlayButton", this.node, Sprite));
        this.menuButtonSps.push(NodeUtil.GetComponent("HeroesButton", this.node, Sprite));

        this.shopPanel = NodeUtil.GetComponent("ShopPanel", this.node.parent, ShopPanel);
        this.heroPanel = NodeUtil.GetComponent("HeroPanel", this.node.parent, HeroPanel);
    }

    protected start(): void {
        if (Banner.NeedShowMoreAd) {
            Banner.Instance.ShowCustomAd();
        }
        Banner.Instance.ShowBannerAd();
        this.RefreshMenu("PlayButton");
        this.RefreshCharacter();
        this.RefreshMoney();
    }

    RefreshCharacter() {
        if (this.hero) PoolManager.PutNode(this.hero);

        ResourceUtil.LoadPrefab(`Characters/${GameManager.Instance.GetSelectedHeroIndex()}`).then((prefab: Prefab) => {
            this.hero = NodeUtil.Instantiate(prefab, director.getScene());
            this.hero.setWorldPosition(v3(0, 1, 0));
            this.hero.setWorldRotation(Quat.IDENTITY);
            this.hero.getComponent(PlayerController).PlayAni(CharacterState.Cel);
            this.hero.getComponent(PlayerController).Display();
        });
    }

    RefreshMenu(name: string) {
        for (let i = 0; i < this.menuButtonSps.length; i++) {
            this.menuButtonSps[i].enabled = this.menuButtonSps[i].node.name == name;
        }

        this.Panel.active = name == "PlayButton";
        this.shopPanel.Show(name == "WeaponsButton");
        this.heroPanel.Show(name == "HeroesButton");
        this.DeckButton.active = name == "WeaponsButton";
    }

    OnButtonClick(event, data) {
        AudioManager.PlaySound(Audios.ButtonClick);

        switch (event.target.name) {

            case 'StartPlayButton':
                // director.loadScene(`Lv_${15}`);
                // return;
                let lv = GameManager.Instance.Lv;
                let scene = "";
                if (lv > 10) {
                    scene = `Lv_${GameManager.RandomLvs[ZTool.GetRandomInt(0, GameManager.RandomLvs.length)]}`
                } else {
                    scene = `Lv_${lv}`
                }

                console.log(`跳转场景`, `Lv_${lv}`);
                director.loadScene(scene);
                break;

            case 'SettingButton':
                UIManager.GetInstance().ShowPanel(Panel.SettingPanel);
                break;

            case 'WeaponsButton':
                this.RefreshMenu(event.target.name);
                break;

            case 'PlayButton':
                this.RefreshMenu(event.target.name);
                break;

            case 'HeroesButton':
                this.RefreshMenu(event.target.name);
                break;

            case 'DeckButton':
                UIManager.GetInstance().ShowPanel(Panel.DeckPanel);
                break;
            case 'PrivacyButton':
                UIManager.GetInstance().ShowPanel(Panel.PrivacyPanel, [false]);
                break;
        }
    }

    RefreshMenusPosition(size: any = null) {
        if (size == null) {
            this.Menus.setPosition(0, -view.getVisibleSize().height / 2 + 233 / 2, 0);
        } else {
            this.Menus.setPosition(0, -view.getVisibleSize().height / 2 + 233 / 2 + size.height, 0);
        }
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.REFRESH_MONEY, this.RefreshMoney, this);
        EventManager.on(Banner.CB_BannerOnSize, this.RefreshMenusPosition, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.REFRESH_MONEY, this.RefreshMoney, this);
        EventManager.off(Banner.CB_BannerOnSize, this.RefreshMenusPosition, this);
    }

    RefreshMoney() {
        this.MoneyLabel.string = `${GameManager.Instance.Money}`;
    }
}